<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title>登录</title>
    <link rel="stylesheet" href="/css/pintuer.css">
    <link rel="stylesheet" href="/css/admin.css">
    <script src="/js/jquery.js"></script>
    <script src="/js/pintuer.js"></script>
    <script src="/js/vuejs-2.5.16.js"></script>
    <script src="/js/axios-0.18.0.js"></script>
</head>
<body>
<div class="bg"></div>
<div class="container" id="app">
    <div class="line bouncein">
        <div class="xs6 xm4 xs3-move xm4-move">
            <div style="height:150px;"></div>
            <div class="media media-y margin-big-bottom">
            </div>
            <form>
                <div class="panel loginbox">
                    <div class="text-center margin-big padding-big-top"><h1>黑马影视后台管理中心</h1></div>
                    <div class="panel-body" style="padding:30px; padding-bottom:10px; padding-top:10px;">
                        <div class="form-group">
                            <div class="field field-icon-right">
                                <input type="text" class="input input-big" name="telephone" v-model="telephone"
                                       placeholder="请输入手机号"/>
                                <span class="icon icon-phone margin-small"></span>
                            </div>
                            <div style="text-align: right">
                                <a  class="button" style="font-size: 14px" @click="sendCode()">发送验证码</a>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="field field-icon-right">
                                <input type="text" class="input input-big" name="code" v-model="code"
                                       placeholder="请输入验证码"/>
                                <span class="icon icon-check margin-small"></span>
                            </div>
                        </div>
                    </div>
                    <div style="padding:30px;"><input type="button" class="button button-block bg-main text-big input-big" @click="login()" value="登录">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    const app = new Vue({
        el: '#app',
        data: {
            telephone:"",
            code:""
        },
        methods: {
            sendCode(){
                //判断手机号是否满足规则,满足规则才发短信
                let reg = /^1[3456789]\d{9}$/;

                if(reg.test(this.telephone)){
                    //发送请求
                    axios.get("/user/sendSms?telephone="+this.telephone).then(resp=>{
                        if(1==resp.data.code){
                            alert("短信已发送");
                        }else{
                            alert(resp.data.msg);
                        }
                    });
                }else{
                    alert("请输入合法的手机号")
                }
            },
            login(){
                //当手机号和验证码都输入之后,发送ajax的post请求完成登陆操作
                if(this.telephone && this.code){
                    axios.post("/user/login",{
                        telephone:this.telephone,
                        code:this.code
                    }).then(resp=>{
                        if (0 == resp.data.code) {
                            alert(resp.data.msg);
                        }else{
                            //其他情况:登陆成功,保存token
                            //localStorage:将数据永久保存到浏览器上
                            //sessionStorage:将数据保存到浏览器的当前会话数据中
                            localStorage.setItem("Authorization",resp.data.data);

                            //跳转到首页
                            location.href="main.html"
                        }
                    });
                }
            }
        },
    })
</script>
</body>
</html>